﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcXRepairAdmin.Interfaces;
using MvcXRepairAdmin.Services;
using XRepair.Models;

namespace MvcXRepairAdmin.Controllers
{
    public class OrderController : BaseController
    {
        private IOrderService _orderService;

        public OrderController()
        {
            _orderService = new OrderService();
        }

        public ActionResult Index()
        {
            IEnumerable<Order> orderList = _orderService.GetAllActiveOrders();

            List<Order> order = orderList != null ? orderList.ToList() : new List<Order>();

            return View(order);
        }

        public ActionResult Delete(int id)
        {
            bool succeed = _orderService.DeactivateOrderByID(id);

            if (succeed)
            {
                return RedirectToAction("Index");
            }

            return RedirectToAction("ErrorOnProcessing", "Error");
        }

        public ActionResult Update(Order model)
        {
            model.LastUpdatedBy = _userSession.Username;
            model.LastUpdatedAt = DateTime.Now;

            bool succeed = _orderService.UpdateOrder(model);

            if (succeed)
            {
                return RedirectToAction("Index");
            }

            return RedirectToAction("ErrorOnProcessing", "Error");
        }

    }
}
